﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MailServer.Recipient {
    [Serializable]
    public class Recipient {
        private MessageList messageList;
        private MessageList readedMessageList;

        public Recipient() {
            messageList = new MessageList();
            readedMessageList = new MessageList();
        }

        public void Add(Message.Message message) {
            messageList.Add(message);
        }

        public int Count(string user) {
            return messageList.Count(user);
        }

        public List<Message.Message> List(string user) {
            return messageList.List(user);
        }

        public List<Message.Message> ListFrom(string user, string from) {
            return messageList.ListFrom(user, from);
        }

        public void Remove(Message.Message message) {
            messageList.Remove(message);
            readedMessageList.Remove(message);
        }

        public void SetReaded(Message.Message message) {
            readedMessageList.Add(message);
        }

        public bool IsReaded(Message.Message message) {
            return readedMessageList.Contains(message);
        }

        public List<Message.Message> ListNew(string user) {
            List<Message.Message> listNew = new List<Message.Message>();
            Message.Message message = GetNextMessage(user);
            while(message != null) {
                listNew.Add(message);
                message = GetNextMessage(user);
            }
            return listNew;
        }

        public Message.Message GetNextMessage(string user) {
            for(int i = 0; i < messageList.Count(user); i++) {
                Message.Message message = messageList.ElementAt(user, i);
                if(!readedMessageList.Contains(message)) {
                    SetReaded(message);
                    return message;
                }
            }
            return null;
        }

    }
}
